import fs from 'fs';
import * as vscode from 'vscode';

// tomcat 
class ConfigUtils {
    file: string = "";
    data: any;
    init(context: vscode.ExtensionContext){
        this.file = context.extensionPath + "/config/tomcat.json";
    }
    getConfig(){
        return this.data;
    }
    addServer(key: string, value: any){
        this.data["server"][key] = value;
        fs.writeFileSync(this.file, JSON.stringify(this.data, null, 4), 'utf-8');
    }
    addDevelop(key: string, value: any){
        this.data["develop"][key] = value;
        fs.writeFileSync(this.file, JSON.stringify(this.data, null, 4), 'utf-8');
    }
    removeServer(key: string){
        delete this.data["server"][key];
        fs.writeFileSync(this.file, JSON.stringify(this.data, null, 4), 'utf-8');
    }
    removeDevelop(key: string){
        delete this.data["develop"][key];
        fs.writeFileSync(this.file, JSON.stringify(this.data, null, 4), 'utf-8');
    }
    flush(){
        
        if(!fs.existsSync(this.file)){
            this.data = JSON.parse(`{"develop":{},"server":{}}`);
        }else{
            this.data = JSON.parse(fs.readFileSync(this.file, 'utf-8'));
        }
    }
}

export default new ConfigUtils();